package com.sgb.link.api;

import android.text.TextUtils;

import com.sgb.link.router.PathLogin;
import com.sgb.link.utils.GsonManager;
import com.sgb.net.entity.BaseEntity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Response;

/**
 * 错误拦截器，做统一错误处理
 */
public class ErrorInterceptor extends ResponseBodyInterceptor {

    @Override
    public Response intercept(@NotNull Response response, String url, String body) throws IOException {
        if (!TextUtils.isEmpty(body)) {
            try {
                BaseEntity entity = GsonManager.getInstance().getGson().fromJson(body, BaseEntity.class);
                //登录失效
                if (entity.getCode()==666) {
                    //重新登录跳转登录页
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, "");
                    ActivityJump.startActivity(PathLogin.VerificationCodeLoginActivity);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }
}
